$(function() {
    var leaveStudent_datagrid =  $("#leaveStudent_datagrid");
    var leaveStudent_dialog =  $("#leaveStudent_dialog");
    var leaveStudent_form =  $("#leaveStudent_form");
    //begin列表信息
    leaveStudent_datagrid.datagrid({
        url: "/leaveStudent/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100,hidden:true},
            {field: "professionalStudent", title: "学员姓名", width: 100,formatter:function (value,row,index) {
                if(value){
                    return row.professionalStudent.name;
                }
                return;
            }},
            {field: "professionalStudent1", title: "QQ", width: 100,formatter:function (value,row,index) {
                        if(row.professionalStudent){

                            return  row.professionalStudent.qq;
                        }

                }
            },
             {field: "professionalStudent2", title: "电话", width: 100,formatter:function (value,row,index) {
                        if(row.professionalStudent){
                         return row.professionalStudent.tell;
                        }

             }},

            {field:'schooltime',title:'上课天数',width:100 ,sortable:true},
            {field:'grade',title:'流失班级',width:100 ,formatter:function (value,row,index) {
                if(value){
                    return value.name
                }
            }},
            {field:'lostState',title:'流失阶段',width:100 ,sortable:true},
            {field:'leavereasion',title:'流失原因',width:100 ,sortable:true},
            {field:'refunddate',title:'流失时间',width:100 },

            {field: "marketer", title: "经办人", width: 100,formatter:function (value,row,index) {
                if(value){
                    return value.username;
                }
                return '';
            }},
            {field: "inputuser", title: "录入人", width: 100,formatter:function (value,row,index) {
                if(value){
                    return value.username
                }
                return "";
            }},
            {field:'refundAmount',title:'退款金额',width:100 },
            {field: "leaceState", title: "审核状态", width: 100,formatter:function (value,row,index) {
                if(value == 0){
                    return '<font color="red">审核中</font>';
                }else if(value == 1){
                    return '<font color="green">已审核</font>';
                }
            }},
            {field: "auditoruser", title: "审核人", width: 100,formatter:function (value,row,index) {
                if(value){
                    return value.username;
                }
             }}
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber:1,
        pageSize:10,
        pageList:[3,5,10,20],
        toolbar: "#leaveStudent_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    leaveStudent_dialog.dialog({
        width: 600,
        height: 400,
        buttons: "#leaveStudent_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin处理功能方法的对象
    var obj = {
        //begin取消按钮
        cancel: function () {
            leaveStudent_dialog.dialog("close");
        },
        //end取消按钮

        //begin查看按钮
        watch: function () {
            var row =leaveStudent_datagrid.datagrid("getSelected");
            console.log(row);
            if(!row){
                $.messager.alert('亲','请选择要查看的数据');
                return;
            }

            //清除数据
            leaveStudent_dialog.form("clear");
            if(row){
                row["professionalStudent.name"] = row.professionalStudent.name;
                row["professionalStudent.qq"] = row.professionalStudent.qq;
               row["professionalStudent.tell"] = row.professionalStudent.tell;
               row["grade.name"] = row.grade.name;
                row["auditoruser.username"] = row.auditoruser.username;
                row["marketer.username"] = row.marketer.username;
                row["inputuser.username"] = row.inputuser.username;
            }
            // 打开窗口
            leaveStudent_dialog.dialog('open');
            //设置文本
            leaveStudent_dialog.dialog("setTitle",'流失学员信息表');
            //数据回显
            leaveStudent_form.form("load",row);

        },
        //end查看按钮

        //begin 审核按钮
        changeState: function (){
            var row = leaveStudent_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('审核确认','想好后请点击确认？',function(r){
                if (r){
                    var url = "/leaveStudent/changeState?id="+row.id;
                    $.get(url, function(data){

                        if (!data.success) {
                            $.messager.alert('温馨提示', data.msg);
                        } else {
                            $.messager.alert('温馨提示', "操作成功!");

                            leaveStudent_datagrid.datagrid("reload");
                        }
                    });
                }
            });

        },
        //end 审核按钮


        // begin流失学员高级查询操作
        query: function() {
            var keywords= $("#keywords").textbox("getValue");
            var beginDate= $("#beginDate").textbox("getValue");
            var endDate= $("#endDate").textbox("getValue");
            var geade= $("#geade").combobox("getValue");
            leaveStudent_datagrid.datagrid("load",{
                keywords:keywords,
                beginDate:beginDate,
                endDate:endDate,
                geade:geade
            });
        },
        // 学员高级查询操作 end

        //begin流失学员取消按钮
        leave: function () {
            leaveStudent_dialog.dialog("close");
        },
        //end流失学员取消按钮

        //begin刷新按钮
        reload:function (){
            leaveStudent_datagrid.datagrid("reload")
        }
        //end刷新按钮
    };
    //end处理功能方法的对象


    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

});